/*
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Created by wang-bain on 2021/11/11.


#include "utils.h"

using namespace std;

int main(int argc, char **argv)
{
    // 全局参数设置, 包含入参列表, 模型信息
    map<char, string> params;
    ModelInfo modelInfo;

    // 读取并校验入参
    if (InitAndCheckParams(argc, argv, params)) return FAILED;

    // ACL接口初始化
    if (InitResource(modelInfo, params)) goto destroy;

    // 加载om模型文件
    if (LoadModelFromFile(modelInfo, params)) goto destroy;

    // 创建模型描述
    if (CreateModelDesc(modelInfo)) goto destroy;

    // 计算输入大小, 用来分配输入内存, 判断是否拼接输入, 补齐不足输入
    if (CalculateInputSize(modelInfo, params)) goto destroy;

    // 循环执行输入目录下所有文件
    for (uint32_t fileIdx = 0; fileIdx < modelInfo.v_allInputFiles[0].size(); fileIdx += modelInfo.v_filesPerBatch[0]) {
        // 创建输入
        if (CreateInput(modelInfo, params, fileIdx)) goto destroy;

        // 创建输入描述文件
        if (CreateInputDesc(modelInfo, params)) goto destroy;

        // 创建输出
        if (CreateOutput(modelInfo, params)) goto destroy;

        // 执行模型
        if (Execute(modelInfo, params, fileIdx)) goto destroy;
    }

    // 写入性能结果
    if (WriteResult(modelInfo, params)) return FAILED;

    // 销毁资源
    destroy:
    if (DestroyResource(modelInfo, params)) return FAILED;

    return SUCCESS;
}